package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.qu_tao.R;
import com.example.qu_tao.provider.TQProvider;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView register;
    private EditText usernameInput, passwordInput;
    private Button loginButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        register = findViewById(R.id.register_link);
        usernameInput = findViewById(R.id.username_input);
        passwordInput = findViewById(R.id.password_input);
        loginButton = findViewById(R.id.login_button);

        register.setOnClickListener(this);
        loginButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.register_link) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        } else if (id == R.id.login_button) {
            String username = usernameInput.getText().toString();
            String password = passwordInput.getText().toString();

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 检查用户名和密码是否匹配
            if (isValidUser(username, password)) {
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                // 获取用户ID并保存
                long userId = getUserId(username, password);
                saveUserId(userId);

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean isValidUser(String username, String password) {
        Uri uri = Uri.parse("content://com.example.qu_tao.provider/user");
        Cursor cursor = getContentResolver().query(uri, null, "user_name = ? AND user_password = ?", new String[]{username, password}, null);
        boolean isValid = cursor != null && cursor.getCount() > 0;
        if (cursor != null) {
            cursor.close();
        }
        return isValid;
    }

    @SuppressLint("Range")
    private long getUserId(String username, String password) {
        Uri uri = Uri.parse("content://com.example.qu_tao.provider/user");
        Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, "user_name = ? AND user_password = ?", new String[]{username, password}, null);
        long userId = -1;
        if (cursor != null && cursor.moveToFirst()) {
            userId = cursor.getLong(cursor.getColumnIndex("_id"));
            cursor.close();
        }
        return userId;
    }

    private void saveUserId(long userId) {
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("user_id", userId);
        editor.apply();
    }
}